<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Tab_posts_module extends MY_Module {
	
	public function __construct() {
		parent::__construct();
		$this->load->model('term_m','term');
		$this->load->model('post_m','post');
	}
	
	public function admin($config) {
		$config['config']['list_cate'] = $this->term->select('term_id, term_name,term_parent')->get_menu_array(0, 'category', '-');
		if (isset($config['config']['cateid']) && $config['config']['cateid'])
			$config['config']['cateid'] = explode(',', $config['config']['cateid']);
		$this->view('admin', $config['config']);
	}
	
	public function module_update($data) {
		if (isset($data['tab_1']['cateid']) && $data['tab_1']['cateid']) {
			$data['tab_1']['cateid'] = implode(',', $data['tab_1']['cateid']);
		}else {
			$data['tab_1']['cateid']  = '';
		}

		if (isset($data['tab_2']['cateid']) && $data['tab_2']['cateid']) {
			$data['tab_2']['cateid'] = implode(',', $data['tab_2']['cateid']);
		}else {
			$data['tab_2']['cateid']  = '';
		}
		return $data;
	}
	/**
	 * 
	 * @param object $config
	 * 
	 */
	public function run($config) {
		$config_default = array(
			'view' => 'view_1',
			'cateid' => '0',
			'tab_1' => array
		        (
		            'title' => '',
		            'title_link' => '',
		            'limit' => '',
		            'order_by' => 'post_time_start',
		            'order' => 'DESC',
		        ),
		    'tab_2' => array
		        (
		            'title' => '',
		            'title_link' => '',
		            'limit' => '',
		            'order_by' => 'post_time_start',
		            'order' => 'DESC',
		        )
		);

		$config = array_merge($config_default,$config);

		if(isset($config['tab_1'])){
			$tmp_config = $config['tab_1'];
			if($tmp_config['limit'] > 50) $tmp_config['limit'] = 50;
			$tmp_config['posts'] = array();
			$params = array(
				'posts_per_page'   => @$tmp_config["limit"],
				'orderby'          => @$tmp_config['order_by'],
				'order'            => @$tmp_config['order'],
				'include'          => @$tmp_config['post_id'],
				'exclude'          => @$tmp_config['ignore_post_id']
			);
			$params['category'] = (isset($tmp_config['cateid'] ) && $tmp_config['cateid'])?explode(',', $tmp_config['cateid']) : '';
			if(isset($tmp_config['iscategory']) && $tmp_config['iscategory'] == 'true')
				if(@$this->msystem->get_global('cat_id') && $this->msystem->get_global('cat_id') != '') 
					$params['category'] = $this->msystem->get_global('cat_id');

			$post_t1 = $this->post->get_deploy_posts($params);
			if($post_t1)
			{
				foreach($post_t1 as $i=>$post)
				{
					$config['post_t1'][$i] = $this->post->process_post_item($post);
					$config['post_t1'][$i]->image = image($post->post_thumb,'post_thumb');
					$config['post_t1'][$i]->image_large = image($post->post_thumb,'thumb_large');
					$config['post_t1'][$i]->alias = str_replace('_', ' ', $post->post_slug);
				}
			}
		}

		if(isset($config['tab_2'])){
			$tmp_config = $config['tab_2'];
			if($tmp_config['limit'] > 50) $tmp_config['limit'] = 50;
			$tmp_config['posts'] = array();
			$params = array(
				'posts_per_page'   => @$tmp_config["limit"],
				'orderby'          => @$tmp_config['order_by'],
				'order'            => @$tmp_config['order'],
				'include'          => @$tmp_config['post_id'],
				'exclude'          => @$tmp_config['ignore_post_id']
			);

			$params['category'] = (isset($tmp_config['cateid'] ) && $tmp_config['cateid'])?explode(',', $tmp_config['cateid']) : '';
			if(isset($tmp_config['iscategory']) && $tmp_config['iscategory'])
				if(@$this->msystem->get_global('cat_id') && $this->msystem->get_global('cat_id') != '') 
					$params['category'] = $this->msystem->get_global('cat_id');

			$posts = $this->post->get_deploy_posts($params);
			if($posts)
			{
				foreach($posts as $i=>$post)
				{
					$config['post_t2'][$i] = $this->post->process_post_item($post);
					$config['post_t2'][$i]->image = image($post->post_thumb,'post_thumb');
					$config['post_t2'][$i]->image_large = image($post->post_thumb,'post_thumb_large');
					$config['post_t2'][$i]->alias = str_replace('_', ' ', $post->post_slug);
				}
			}
		}
		$this->view($config['view'], $config);
	}
}